import logging
import model

DEFAULT_RATING = 1500


def update_rating(winner, loser):
  logging.debug("winner : " + winner.email + "," +  str(winner.rating))
  logging.debug("loser: " + loser.email + "," + str(loser.rating))
  
  diff = winner.rating - loser.rating
  ave_rating = (winner.rating + loser.rating) / 2
  new_winner_rating = winner.rating + ave_rating/30 - diff/50
  if (new_winner_rating < winner.rating):
      new_winner_rating = winner.rating
  new_loser_rating = loser.rating - ave_rating/30 + diff/50
  if (new_loser_rating > loser.rating):
      new_loser_rating = loser.rating
  winner.rating = new_winner_rating
  loser.rating = new_loser_rating
  winner.number_of_games_played += 1
  loser.number_of_games_played += 1
  logging.debug("ratings updated:")
  logging.debug("winner : " + winner.email + "," + str(winner.rating))
  logging.debug("loser: " + loser.email + "," + str(loser.rating))
  winner.put()
  loser.put()
  